package com.ditian.http.apienc;

import com.alibaba.fastjson.JSONObject;
import com.ditian.code.ErrorMessage;
import com.ditian.http.Key;
import com.ditian.http.Response;
import com.ditian.http.api.CommonApi;
import com.ditian.ret.CompareRet;
import com.ditian.ret.DetectRet;
import com.ditian.ret.SearchRet;

public class CommonApiEnc {

	private static CommonApi api = new CommonApi(Key.API_KEY, Key.API_SECRET,
			Key.ISINTERNATIONALVERSION);

	/**
	 * 调用者提供图片URL，进行人脸检测。 detect api through network image url
	 * 
	 * @param imageUrl
	 *            图片链接 image url
	 * @param landmark
	 *            是否返回人脸的关键点，1：返回，0：不返回 Whether or return 83 key points of
	 *            facial features and contour，1：return，0：not return
	 * @param attributes
	 *            检测人脸的属性 gender,age,smiling,glass,headpose,facequality,blur
	 *            detect face attributes:
	 *            gender,age,smiling,glass,headpose,facequality,blur
	 * @return
	 * @throws Exception
	 */
	public static DetectRet detectUrl(String imageUrl, int landmark,
			String attributes) {
		try {
			Response response = api.detectUrl(imageUrl, landmark, attributes);
			if (response != null) {
				String json = response.getJson();
				int status = response.getStatus();
				DetectRet ret = JSONObject.parseObject(json, DetectRet.class);
				if (status != 200 && ret != null) {
					ret.setError(ErrorMessage.messageOf(status,
							ret.getErrorMessage()));
				}
				return ret;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 调用者提供图片文件，进行人脸检测。 detect api through native image finle
	 * 
	 * @param fileByte
	 *            二进制数组 image binary array
	 * @param landmark
	 *            是否返回人脸的关键点，1：返回，0：不返回 Whether or return 83 key points of
	 *            facial features and contour，1：return，0：not return
	 * @param attributes
	 *            检测人脸的属性 gender,age,smiling,glass,headpose,facequality,blur
	 *            detect face attributes:
	 *            gender,age,smiling,glass,headpose,facequality,blur
	 * @return
	 * @throws Exception
	 */
	public static DetectRet detectByte(byte[] fileByte, int landmark,
			String attributes) {
		try {
			Response response = api.detectByte(fileByte, landmark, attributes);
			if (response != null) {
				String json = response.getJson();
				int status = response.getStatus();
				DetectRet ret = JSONObject.parseObject(json, DetectRet.class);
				if (status != 200 && ret != null) {
					ret.setError(ErrorMessage.messageOf(status,
							ret.getErrorMessage()));
				}
				return ret;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 调用者提供图片文件，进行人脸检测。 detect api through native image finle
	 * 
	 * @param base64
	 *            Base64数据 image data for base64
	 * @param landmark
	 *            是否返回人脸的关键点，1：返回，0：不返回 Whether or return 83 key points of
	 *            facial features and contour，1：return，0：not return
	 * @param attributes
	 *            检测人脸的属性 gender,age,smiling,glass,headpose,facequality,blur
	 *            detect face attributes:
	 *            gender,age,smiling,glass,headpose,facequality,blur
	 * @return
	 * @throws Exception
	 */
	public static DetectRet detectBase64(String base64, int landmark,
			String attributes) {
		try {
			Response response = api.detectBase64(base64, landmark, attributes);
			if (response != null) {
				String json = response.getJson();
				int status = response.getStatus();
				DetectRet ret = JSONObject.parseObject(json, DetectRet.class);
				if (status != 200 && ret != null) {
					ret.setError(ErrorMessage.messageOf(status,
							ret.getErrorMessage()));
				}
				return ret;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将两个人脸进行比对，来判断是否为同一个人。 compare two faces
	 * 
	 * @param faceToken1
	 *            第一个人脸标识face_token first face_token
	 * @param image_url1
	 *            第一个人脸的url image url of first face
	 * @param fileByte1
	 *            第一个人脸的图片文件 file of first face 三个参数只需要传一个就行了 only need one of
	 *            three parameter
	 * 
	 * @param faceToken2
	 *            第二个人脸标识face_token second face_token
	 * @param image_url2
	 *            第二个人脸的url image url of second face
	 * @param fileByte2
	 *            第二个人脸的图片文件 file of second face 三个参数只需要传一个就行了 only need one of
	 *            three parameter
	 * @return
	 * @throws Exception
	 */
	public static CompareRet compare(String faceToken1, String image_url1,
			byte[] fileByte1, String base64_1, String faceToken2,
			String image_url2, byte[] fileByte2, String base64_2) {

		try {
			Response response = api.compare(faceToken1, image_url1, fileByte1,
					base64_1, faceToken2, image_url2, fileByte2, base64_2);
			if (response != null) {
				String json = response.getJson();
				int status = response.getStatus();
				CompareRet ret = JSONObject.parseObject(json, CompareRet.class);
				if (status != 200 && ret != null) {
					ret.setError(ErrorMessage.messageOf(status,
							ret.getErrorMessage()));
				}
				return ret;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 在Faceset中找出与目标人脸最相似的一张或多张人脸。 search the Most similar from FaceSet
	 * faceToken,image_url,image_file,buff四个参数只要传入一个就可以了，其他可以传空（null） only need
	 * one of faceToken,image_url,image_file,buff
	 * 
	 * @param faceToken
	 *            与Faceset中人脸比对的face_token Identification of face
	 * @param image_url
	 *            需要比对的人脸的网络图片URL network image url
	 * @param buff
	 *            需要比对的人脸的图片的二进制数组 native image
	 * @param faceSetToken
	 *            Faceset的标识 Identification of faceSet
	 * @param returnResultCount
	 *            返回比对置信度最高的n个结果，范围[1,5]。默认值为1 the number of result,
	 *            1-5,Defaults to 1
	 * @return
	 * @throws Exception
	 */
	public static SearchRet searchByFaceSetToken(String faceToken,
			String image_url, byte[] buff, String faceSetToken,
			int returnResultCount) {
		try {
			Response response = api.searchByFaceSetToken(faceToken, image_url,
					buff, faceSetToken, returnResultCount);
			if (response != null) {
				String json = response.getJson();
				int status = response.getStatus();
				SearchRet ret = JSONObject.parseObject(json, SearchRet.class);
				if (status != 200 && ret != null) {
					ret.setError(ErrorMessage.messageOf(status,
							ret.getErrorMessage()));
				}
				return ret;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 在Faceset中找出与目标人脸最相似的一张或多张人脸。 search the Most similar from FaceSet
	 * faceToken,image_url,image_file,buff四个参数只要传入一个就可以了，其他可以传空（null） only need
	 * one of faceToken,image_url,image_file,buff
	 * 
	 * @param faceToken
	 *            与Faceset中人脸比对的face_token Identification of face
	 * @param image_url
	 *            需要比对的人脸的网络图片URL network image url
	 * @param buff
	 *            需要比对的人脸的图片的二进制数组 native image
	 * @param outerId
	 *            Faceset的标识 Identification of faceSet which is definition by
	 *            youself
	 * @param returnResultCount
	 *            返回比对置信度最高的n个结果，范围[1,5]。默认值为1 the number of result,
	 *            1-5,Defaults to 1
	 * @return
	 * @throws Exception
	 */
	public static SearchRet searchByOuterId(String faceToken, String image_url,
			byte[] buff, String outerId, int returnResultCount) {
		try {
			Response response = api.searchByOuterId(faceToken, image_url, buff,
					outerId, returnResultCount);
			if (response != null) {
				String json = response.getJson();
				int status = response.getStatus();
				SearchRet ret = JSONObject.parseObject(json, SearchRet.class);
				if (status != 200 && ret != null) {
					ret.setError(ErrorMessage.messageOf(status,
							ret.getErrorMessage()));
				}
				return ret;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
